<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Cart as cartModel;
class Cart extends Controller
{
	public function query(){
		$res = Db::query('select sId,cId,selected,goodsNum,sName,price,gName,img from goodsskuimg right join 
		(select sId,cId,selected,goodsNum,sName,price,name as gName from goods right join 
		(select sId,cId,selected,goodsNum,name as sName,price,goodsId from goodssku right join 
		(select selected,goodsNum,id as cId,goodsSkuId as sId from cart) c
		on c.sId=goodssku.id) cs
		on cs.goodsId = goods.id) csg
		on csg.sId = goodsskuimg.goodsskuid');
		
		return json(['code'=>200,'data'=>$res]);
	}
  
    public function add()
    {
        //
		$goodsSkuId =input('post.goodsSkuId');
		$goodsNum = input('post.goodsNum');
		$userId = input('post.userId');
		$selected = input('post.selected');
		$r = Db::execute('insert into cart (id,goodsSkuId,goodsNum,userId,selected) values (null,?,?,?,?)',[$goodsSkuId,$goodsNum,$userId,$selected]);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

  
    public function updateSelected()
    {
        $id = input('post.id');
		$r = Db::query('update cart set selected=selected ^ 1 where id=?',[$id]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        //
		$cart = new cartModel();
		$cart->goodsSkuId = 1;
		$cart->goodsNum = 1;
		$cart->userId = 1;
		$cart->save();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
